package com.hao.ui;

import com.hao.enums.ComponentEnum;
import com.hao.listener.MyActionListener;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.io.File;
import java.net.URL;

/**
 * Created by Steven on 2017/7/22.
 */
public class MainActivity {



    public void init() {
        // 确保一个漂亮的外观风格
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        // 创建及设置窗口
        JFrame frame = new JFrame("weibo-tool");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(null);
        URL url = ClassLoader.getSystemResource("images/QQ.png");
        System.out.print(url.getPath());
        ImageIcon icon = new ImageIcon(url.getPath());
        frame.setIconImage(icon.getImage());
        frame.setSize(900, 660);
        // 定义总面板
        JPanel panel_content = new JPanel();
        panel_content.setBounds(0,0,900,600);
        panel_content.setLayout(null);

        // 定义标题面板
        JPanel pane_title = new JPanel();
        pane_title.setLayout(null);
        pane_title.setBounds(10,0,890,30);

        // 定义标题
        this.jLabel_title = createLabel("欢迎使用接口测试工具定制版1.0",0,0,890,30);
        pane_title.add(jLabel_title);
        pane_title.setBackground(Color.blue);
        panel_content.add(pane_title);

        // 定义url面板
        JPanel pane_url = new JPanel();
        pane_url.setLayout(null);
        pane_url.setBounds(10,40,890,30);

        // 定义请求方法下拉框
        String[] options = {"POST"};
        comboBox_request_type = createComboBox(0,0,80,30, options);
        pane_url.add(comboBox_request_type);

        // 定义url输入框
        textField_url = createTextField(90,0,500,30);
        pane_url.add(textField_url);

        // 定义发送按钮
        pane_url.add(createButton("发送", 620, 0, 60, 30, ComponentEnum.BUTTON_SEND));
        panel_content.add(pane_url);

        // 定义测试按钮
        pane_url.add(createButton("测试", 690, 0, 60, 30, ComponentEnum.BUTTON_TEST));
        panel_content.add(pane_url);

        // 定义停止按钮
        pane_url.add(createButton("重启", 760, 0, 60, 30, ComponentEnum.BUTTON_RESTART));
        panel_content.add(pane_url);


        // 定义请求header面板
        Object[][] defaultInfo = {
                {new Boolean(true),  "Cookie", "UM_distinctid=15c42d03b62178-0b1bddedc83ed-37624605-15f900-15c42d03b63ce7; SINAGLOBAL=9797632620784.018.1495769562353; CNZZDATA1261337656=834542845-1500264088-http%253A%252F%252Fweibo.com%252F%7C1500264088; wb_publish_fist100_2064600267=1; wvr=6; wb_cmtLike_2064600267=1; YF-Ugrow-G0=57484c7c1ded49566c905773d5d00f82; login_sid_t=315701ddc45dcea7e27f4b0a1d395a9c; YF-V5-G0=b1e3c8e8ad37eca95b65a6759b3fc219; WBStorage=cd7f674a73035f73|undefined; _s_tentry=-; UOR=www.baidu.com,vdisk.weibo.com,www.2345.com; Apache=3192204648846.322.1500857639961; ULV=1500857639965:11:8:1:3192204648846.322.1500857639961:1500635863915; SSOLoginState=1500857728; SCF=Akxn-xztC4aOW74q_GyNaSARKnOh52oIaZ6Z1Fkv1ud7O9phsvV-jsK0yhP-d_5gQU6BkD00pfpXiyn8_CuP_k4.; SUB=_2A250cTXQDeRhGeRO7VYX8C7OzTuIHXVXByAYrDV8PUNbmtBeLWqgkW-gMaSbsEaAc1J3g2mq6nZTi7EWZw..; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WW69Xd_SFE.5yf4jS8v5F9r5JpX5K2hUgL.Foz7SoBceh5ESoM2dJLoIEXLxKBLBonL12BLxKMLB.-L1h2LxKML1-2L1hBLxKMLB-eL1K2LxKML1hzLBo.t; SUHB=08JKuyCQ4w_Hdj; ALF=1532393727; un=781582206@qq.com; YF-Page-G0=b9004652c3bb1711215bacc0d9b6f2b5"},
                {new Boolean(true), "Accept", "*/*"},
                {new Boolean(true),  "Accept-Encoding", "gzip, deflate"},
                {new Boolean(true),  "Accept-Language", "zh-CN,zh;q=0.8"},
                {new Boolean(true),  "Connection", "keep-alive"},
                {new Boolean(true),  "Content-Type", "application/x-www-form-urlencoded"},
                {new Boolean(true),  "Host", "weibo.com"},
                {new Boolean(true),  "Origin", "http://weibo.com"},
                {new Boolean(true),  "Referer", "http://weibo.com/u/2064600267/home?wvr=5"},
                {new Boolean(true),  "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"},
                {new Boolean(true),  "X-Requested-With", "XMLHttpRequest"}
        };
        String[] defaultNames = { "isUse", "Key", "Value"};

        tableModel_header = new DefaultTableModel(defaultInfo, defaultNames); // 新建一个默认数据模型
        JTable table_header = new JTable(tableModel_header);
        TableColumn tc1 = table_header.getColumnModel().getColumn(0);
        tc1.setCellEditor(new DefaultCellEditor(new JCheckBox()));
        table_header.setRowHeight(25);
        JScrollPane jscrollPane_header = new JScrollPane(table_header);
        jscrollPane_header.setBounds(10,80,870,153);
        panel_content.add(jscrollPane_header);

        JPanel panel_add_button = new JPanel();
        panel_add_button.setLayout(null);
        panel_add_button.setBounds(10, 238, 870, 30);

        panel_add_button.add(createButton("add header",0, 0, 120, 30, ComponentEnum.BUTTON_ADD_HEADER));
        panel_content.add(panel_add_button);

        // 创建请求提面板
        // 以Names和playerInfo为参数，创建一个表格
        Object[][] requestInfo = {
                {new Boolean(true), "location", "v6_content_home"},
                {new Boolean(true), "appkey", ""},
                {new Boolean(true), "style_type", 1},
                {new Boolean(true), "pic_id", ""},
                {new Boolean(true), "tid", ""},
                {new Boolean(true), "pdetail", ""},
                {new Boolean(true), "rank", 0},
                {new Boolean(true), "rankid", ""},
                {new Boolean(true), "module", "stissue"},
                {new Boolean(true), "pub_source", "main_"},
                {new Boolean(true), "pub_type", "dialog"},
                {new Boolean(true), "_t", 0},
        };
        tableModel_request = new DefaultTableModel(requestInfo, defaultNames); // 新建一个默认数据模型
        JTable table_request = new JTable(tableModel_request);
        TableColumn tableColumn_request = table_request.getColumnModel().getColumn(0);
        tableColumn_request.setCellEditor(new DefaultCellEditor(new JCheckBox()));

        table_request.setRowHeight(25);
        JScrollPane jscrollPane_request = new JScrollPane(table_request);
        jscrollPane_request.setBounds(10,278,870,153);
        panel_content.add(jscrollPane_request);

        JPanel panel_add_request_button = new JPanel();
        panel_add_request_button.setLayout(null);
        panel_add_request_button.setBounds(10, 436, 870, 30);

        panel_add_request_button.add(createButton("add request",0, 0, 120, 30, ComponentEnum.BUTTON_ADD_REQUEST));
        panel_content.add(panel_add_request_button);

        // 定义结果显示文本及面板
        textArea_res = new JTextArea("返回结果在这里");
        textArea_res.setBounds(0,0,870,150);
        textArea_res.setLineWrap(true);
        textArea_res.setEditable(false);
        JScrollPane jscrollPane_res = new JScrollPane(textArea_res);
        jscrollPane_res.setBounds(10,476,870,150);

        panel_content.add(jscrollPane_res);

        frame.add(panel_content);
        // 显示窗口
//        frame.pack();
        frame.setVisible(true);
    }

    private JLabel createLabel(String text, Integer x, Integer y, Integer width, Integer height) {
        JLabel label = new JLabel(text);
        label.setBounds(x, y, width, height);
        return label;
    }

    private JComboBox createComboBox(Integer x, Integer y, Integer width, Integer height, String[] options) {
        JComboBox comboBox = new JComboBox();
        for(String option : options) {
            comboBox.addItem(option);
        }
        comboBox.setBounds(x,y,width,height);
        return comboBox;
    }

    private JTextField createTextField(Integer x, Integer y, Integer width, Integer height) {
        JTextField textField = new JTextField("http://weibo.com/aj/mblog/add?ajwvr=6&__rnd=");
        textField.setBounds(x, y, width, height);
        return textField;
    }

    private JButton createButton(String text, Integer x, Integer y, Integer width, Integer height, ComponentEnum componentEnum) {
        JButton button = new JButton();
        button.setText(text);
        button.setBounds(x,y,width,height);
        button.addActionListener(new MyActionListener(componentEnum, this));
        return button;
    }

    private JLabel jLabel_title;

    private JTextField textField_url;

    private JComboBox comboBox_request_type;

    private JTextArea textArea_res;

    private DefaultTableModel tableModel_header;

    private DefaultTableModel tableModel_request;

    public DefaultTableModel getTableModel_request() {
        return tableModel_request;
    }

    public void setTableModel_request(DefaultTableModel tableModel_request) {
        this.tableModel_request = tableModel_request;
    }

    public DefaultTableModel getTableModel_header() {
        return tableModel_header;
    }

    public void setTableModel_header(DefaultTableModel tableModel_header) {
        this.tableModel_header = tableModel_header;
    }

    public JTextArea getTextArea_res() {
        return textArea_res;
    }

    public JComboBox getComboBox_request_type() {
        return comboBox_request_type;
    }

    public JTextField getTextField_url() {
        return textField_url;
    }

    public JLabel getjLabel_title() {
        return jLabel_title;
    }
}
